package com.ipt.frm.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ipt.frm.beans.Mensaje;
import com.ipt.frm.enumeraciones.TipoMensaje;
import com.ipt.frm.userAgent.UAgentInfo;

/**
 * Servlet Filter implementation class FiltroMaestro
 */
public class FiltroMaestro implements Filter {

	public FilterConfig filterConfig;

	private ArrayList<String> urlList;

	private UAgentInfo uAgentInfo;

	private boolean isMobile;

	/**
	 * @see Filter#destroy()
	 */
	@Override
	public void destroy() {

	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpreq = (HttpServletRequest) request;
		HttpServletResponse httpres = (HttpServletResponse) response;
		// System.out.println(httpreq.getHeader("User-Agent"));
		// System.out.println(httpreq.getHeader("Accept"));
		uAgentInfo = new UAgentInfo(httpreq.getHeader("User-Agent"),
				httpreq.getHeader("Accept"));
		// System.out.println(uAgentInfo.detectMobileQuick());
		isMobile = uAgentInfo.detectMobileQuick();
		String url = httpreq.getServletPath();
		boolean allowedRequest = false;

		if (urlList.contains(url)) {
			allowedRequest = true;
		}
		System.out.println("Pasa");
		if (isMobile) {
			if (!allowedRequest) {
				httpreq.getSession().getServletContext()
						.getRequestDispatcher("/movil/index.jsp")
						.forward(request, response);
			} else {
				chain.doFilter(request, response);
			}

			// httpres.sendRedirect("/PruebasWeb2/movil/index.jsp");
		} else {

			if ((!allowedRequest)
					&& (httpreq.getSession().getAttribute("username") == null)) {
				Properties propertiesMsg = (Properties) httpreq.getSession()
						.getServletContext().getAttribute("mensajes");
				Mensaje msg = new Mensaje(TipoMensaje.ALERT,
						propertiesMsg.getProperty("login.noLogin"));
				request.setAttribute("MSG", msg);
				request.setAttribute("titulo", "Login");
				request.getRequestDispatcher("/login").forward(request,
						response);
			} else {
				chain.doFilter(request, response);
			}

		}

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		this.filterConfig = fConfig;
		String urls = fConfig.getInitParameter("avoid-urls");
		StringTokenizer token = new StringTokenizer(urls, ",");

		urlList = new ArrayList<String>();

		while (token.hasMoreTokens()) {
			urlList.add(token.nextToken());

		}
	}

}
